import { _decorator, Component, Label, math, Node } from 'cc';
import { VirtualList } from './VirtualList';
const { ccclass, property } = _decorator;

@ccclass('main')
export class main extends Component {

    @property(VirtualList)
    list: VirtualList = null!;
    data: { type: number }[] = [];

    start() {
        this.list.itemRender = this.onItemRender.bind(this);
        this.list.getItemResIndex = this.getItemResIndex.bind(this);
        this.list.onItemClick = this.onItemClick.bind(this);
        for (let i = 0; i < 500; i++) {
            this.data.push({ type: math.randomRangeInt(0, this.list.itemPrefabs.length) })
        }
        this.list.setData(this.data);
    }

    private onItemRender(index: number, node: Node) {
        node.getChildByName("Label").getComponent(Label).string = index.toString();
    }

    private getItemResIndex(index: number) {
        return this.data[index].type;
    }

    private onItemClick(index: number, node: Node) {
        console.log("点击下标:" + index);
    }
}


